__author__ = "Abhinav Jha"
__credits__ = ["Abhinav Jha"]
__version__ = "0.1"
__maintainer__ = "Abhinav Jha"
__email__ = "abhinav@knowlarity.com"
__status__ = "Development"


from twisted.internet import reactor
from twisted.internet import stdio
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.protocols import basic
from twisted.words.protocols import irc

import sys

class learnerKun(irc.IRCClient):
    def __init__(self,*args, **kwargs):
        pass

    def get_channel(self):
        return self.factory.channel
    def set_channel(self, channel):
        self.factory.channel = channel

    def get_nick(self):
        return self.factory.nickname
    def set_nick(self, nickname):
        self.factory.nickname = nickname


    nickname = property(get_nick, set_nick)
    channel = property(get_channel, set_channel)

    def signedOn(self):
        print "Joining %s" % (self.channel)
        self.factory.curr_instance = self
        self.join(self.factory.channel)

    def joined(self, channel):
        print "Joined %s" % (channel)

    def privmsg(self, user, channel, message):
        if channel != self.factory.nickname:
            print "[%s] : %s" % (user.split('!')[0], message)
            print ">>> "
            if self.factory.nickname in message:pass
            #else:self.humanize(user, channel, message)
        self.add_to_brain(message)
        reactor.callLater(1800, self.spew_random_line)

    def add_to_brain(self, message):
        fp = file(self.factory.hippocampus, "a")
        fp.write(message)
        fp.write("\n")
        fp.close()

    def spew_random_line(self):
        import random
        rand_int = random.randint(1, 100)
        lines = file(self.factory.hippocampus).readlines()
        self.msg(self.factory.channel, lines[rand_int])

    def userLeft(self, user, channel):pass
    def userJoined(self, user, channel):pass
    def userRenamed(self, userOld, userNew):pass
    def humanize(self, user, channel, message):pass


class Echo(basic.LineReceiver):
    from os import linesep as delimiter
    def __init__(self, *args, **kwargs):
        self.lkf = ""

    def connectionMade(self):
        self.transport.write('>>> ')

    def lineReceived(self, line):
        if line.strip():
            self.lkf.curr_instance.msg(self.lkf.channel, line)
            self.transport.write("[%s] : %s\n" % (self.lkf.nickname, line))
        self.transport.write('>>> ')

class learnerKunFactory(ClientFactory):
    protocol = learnerKun
    def __init__(self, channel="#india", nickname="|Zoro|"):
        self.channel = channel
        self.nickname = nickname
        self.friendlist = []
        self.curr_instance = ""
        self.hippocampus = "hippocampus.txt"

    def clientConnectionLost(self, connector, reason):
        print "Lost connection, %s" % (reason)

    def clientConnectionFailed(self, connector, reason):
        print "Could not connect, %s" % ( reason )

channel = "#india"

# The reactor.connectTCP() connects a factory instance to a server.
echo_instance = Echo()
lkf_instance = learnerKunFactory(channel)
echo_instance.lkf = lkf_instance

stdio.StandardIO(echo_instance)
reactor.connectTCP('irc.dal.net', 6667, lkf_instance)

# Starting the  event loop
reactor.run()
